<?php

namespace app\adminabc\model;

use think\Model;

class Role extends Model {
	function getRole() {
		return db ( 'role' )->order ( 'id desc' )->select ();
	}
	function tree() {
		$arr1 = db ( 'node' )->where ( array ([ 'pid','=',0 ],[ 'level','=',1 ] ) )->order ( 'sort desc,id desc' )->select ();
		foreach ( $arr1 as $k => $v ) {
			$arr2 = db ( 'node' )->where ( array ([ 'pid','=',$v ['id'] ],[ 'level','=',2 ] ) )->order ( 'sort desc,id desc' )->select ();
			foreach ( $arr2 as $kk => $vv ) {
				$arr1 [$k] ['item'] [] = $vv;
				$arr3 = db ( 'node' )->where ( array ([ 'pid','=',$vv ['id'] ],[ 'level','=',3 ] ) )->order ( 'sort desc,id desc' )->select ();
				foreach ( $arr3 as $kkk => $vvv ) {
					$arr1 [$k] ['item'] [$kk] ['item'] [] = $vvv;
				}
			}
		}
		
		return $arr1;
	}
	function add($data) {
		return db ( 'role' )->insertGetId ( $data );
	}
	function edit($id, $data) {
		return db ( 'role' )->where ( array ([ 'id','=',$id ] ) )->update ( $data );
	}
	function del($map) {
		return db ( 'role' )->where ( $map )->delete ();
	}
	function roleInfo($map) {
		return db ( 'role' )->where ( $map )->find ();
	}
	function getAccess($map) {
		$pre = config ( 'database.prefix' );
		return db ( 'access a' )->leftJoin ( "{$pre}role_user u", "u.role_id=a.role_id" )->leftJoin ( "{$pre}node n", "n.id=a.node_id" )->where ( $map )->field ( 'a.*' )->find ();
	}
}

?>